package com.jichangxiu.dynamic.test.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * @author Jcx
 * @date 2022/11/27 7:51
 */
@Slf4j
public class DynamicDataSource extends AbstractRoutingDataSource {

    public static final String DEFAULT_DB_KEY = "t0";

    @Override
    protected Object determineCurrentLookupKey() {
        String currentDb = DynamicDataSourceService.currentDb();
        if (currentDb == null) {
            currentDb = DEFAULT_DB_KEY;
            log.info("currentDb:" + currentDb);
            return currentDb;
        }
        return currentDb;
    }

}

